﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using digitwhu.Models;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using digitwhu.Helper;

namespace digitwhu.Models
{
    public class NodeRepository
    {
        //digitwhu database context
        DigitwhuDataContext db;

        public NodeRepository()
        {
            db = new DigitwhuDataContext();
        }

        public NodeStruct getNode(int id)
        {
            NodeStruct node = new NodeStruct(id);

            //查询node节点下的paths
            var queryPath = from v in db.View_node_path_street
                       where v.node_id == id
                       select v;
            foreach (var path in queryPath)
            {
                if (path.path_id == null)
                    continue;
                int path_id = (int)path.path_id;
                PathStruct p = new PathStruct(path_id, path.path_type, path.path_north, path.path_south, path.path_west, path.path_east, path.street_id, path.street_name);
                node.paths.Add(p);

                //查询path节点下的buildings
                var queryBuilding = from v in db.View_path_building
                                    where v.path_id == path_id
                                    select v;
                foreach (var building in queryBuilding)
                {
                    if (building.building_id == null)
                        continue;
                    int building_id = (int)building.building_id;
                    BuildingStruct b = new BuildingStruct(building_id, building.building_name, building.building_type, building.building_arrange);
                    p.buildings.Add(b);
                }
            }

            
            return node;
        }

        public List<DormStruct> getDorms()
        {
            List<DormStruct> dorms = new List<DormStruct>();
            var query = from building in db.building
                        where building.type == "dorm"
                        select building;
            foreach(var building in query)
            {
                DormStruct dorm = new DormStruct();
                dorm.dormId = building.id.ToString();
                dorm.dormName = building.name;
                dorm.pathId = building.path_id.ToString();
                dorm.arrange = building.arrange.ToString();
                dorms.Add(dorm);
            }
            return dorms;
        }


        
    }
    /// <summary>
    /// node节点结构
    /// </summary>
    [DataContract]
    public class NodeStruct
    {
        [DataMember]
        public int node_id;
        [DataMember]
        public IList<PathStruct> paths;

        public NodeStruct()
        {
            this.paths = new List<PathStruct>();
        }
        public NodeStruct(int node_id)
            : this()
        {
            this.node_id = node_id;
        }
 
    }
    /// <summary>
    /// path节点结构
    /// </summary>
    [DataContract]
    public class PathStruct
    {
        [DataMember]
        public int path_id;
        [DataMember]
        public String type;
        [DataMember]
        public int? north;
        [DataMember]
        public int? south;
        [DataMember]
        public int? west;
        [DataMember]
        public int? east;
        [DataMember]
        public int? street_id;
        [DataMember]
        public String street_name;
        [DataMember]
        public IList<BuildingStruct> buildings = new List<BuildingStruct>();

        public PathStruct()
        {
            this.buildings = new List<BuildingStruct>();
        }
        public PathStruct(int path_id, String type, int? north, int? south, int? west, int? east, int? street_id, String street_name)
            : this()
        {
            this.path_id = path_id;
            this.type = type;
            this.north = north;
            this.south = south;
            this.west = west;
            this.east = east;
            this.street_id = street_id;
            this.street_name = street_name;
        }
    }
    /// <summary>
    /// building节点结构
    /// </summary>
    [DataContract]
    public class BuildingStruct
    {
        [DataMember]
        public int building_id;
        [DataMember]
        public String name;
        [DataMember]
        public String type;
        [DataMember]
        public int? arrange;

        public BuildingStruct(int building_id, String name, String type, int? arrange)
        {
            this.building_id = building_id;
            this.name = name;
            this.type = type;
            this.arrange = arrange;
        }

    }

    //宿舍信息
    [DataContract]
    public class DormStruct
    {
        [DataMember]
        public String dormId;
        [DataMember]
        public String dormName;
        [DataMember]
        public String pathId;
        [DataMember]
        public String arrange;

    }
}